<template>
    <div class="login">
        <el-card class="box-card">
        <el-form ref="form" :model="formInline" class="demo-form-inline" :rules="rules">
            <el-form-item label="账号：" prop="user">
                <el-input v-model="formInline.user" placeholder="请输入账号" />
            </el-form-item>
            <el-form-item label="密码：" prop="password">
                <el-input type="password" v-model="formInline.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">登录</el-button>
            </el-form-item>
        </el-form>
        </el-card>
    </div>
</template>

<script>
export default {
    name: "Login",
    data() {
        return {
            formInline: {
                user: '',
                password: ''
            },
            rules: {
                user: [
                    {
                        required: true,
                        message: "请输入账号",
                        type:"string",
                        trigger: "blur"
                    }
                ],
                password: [
                    {
                        required: true,
                        message: "请输入密码",
                        type:"string",
                        trigger:"blur"
                    }
                ]
            }
        }
    },
    methods: {
        onSubmit() {
            // console.log('submit!', this.$refs.form);
            this.$refs.form.validate((validate)=>{
                console.log(validate);
                if (validate) {
                    // 通过验证
                    this.$router.push('/index')
                    localStorage.setItem('token', '1')
                }else {
                    // 未通过验证
                    this.$message({
                        message: '请输入账号和密码',
                        type: 'error',
                    })
                    localStorage.removeItem('token')
                }
            });
            
            // this.$router.push('/index')
        }
    }
}
</script>

<style scoped>
.login {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>